/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tetris;

import java.util.LinkedList;

/**
 *
 * @author CJ
 */
public class Board {
    
    protected int[][] board;
    protected int nextid;
    protected LinkedList<Block> allBlocks;

    public Board(int x, int y)
    {
        board = new int[y + 1][x];
        for(int row = 0;row<board.length;row++)
            for(int col = 0;col<board[row].length;col++)
                board[row][col]=-1;
        nextid = 0;
        allBlocks = new LinkedList();
    }

    public Board()
    {
        board = new int[51][30];
        for(int row = 0;row<board.length;row++)
            for(int col = 0;col<board[row].length;col++)
                board[row][col]=-1;
        nextid = 0;
        allBlocks = new LinkedList();
    }

    public int[][] getBoard()
    {
        return board;
    }

    public void put(Block b)
    {
        for(int y = 0; y < b.getBoundry().length; y ++)
        {
            board[b.getBoundry()[y][0] - 1][b.getBoundry()[y][1]] = nextid;
        }
        nextid++;
        allBlocks.add(b);
    }


    /**
     *
     * @param b
     * @return false if the Block cannont move, true if the block can move
     */
    public boolean move(Block b)
    {
        for(int y = 0; y < b.getBoundry().length;y++)
        {
            if(board[b.getBoundry()[y][0]]
                    [b.getBoundry()[y][1]] != -1)
            {
                return false;
            }
        }
        return true;
    }

    public int clearRows(LinkedList currentBlocks)
    {
        int numberFount = 0;
        boolean flag = true;
        for(int row = board.length - 2;row > 0;row--)
        {
            for(int col = 0;col<board[row].length;col++)
            {
                if(board[row][col] == -1)
                    flag = false;
            }

            if(flag)
            {
                numberFount++;
                //Find ids of blocks missing
                LinkedList<Integer> ids = new LinkedList();
                for(int col = 0;col < board[row].length;col++)
                    if(!ids.contains(board[row][col]))
                        ids.add(board[row][col]);

                //Remove from current List, change the others to orhpaned blocks
                for(int i = 0; i < ids.size();i++)
                {
                    currentBlocks.remove(allBlocks.get(ids.get(i)));
                    for(int rows = board.length - 2;rows>0;rows--)
                    {
                        if(rows == row)
                            continue;
                        for(int col = 0;col<board[rows].length;col++)
                        {
                            if(board[rows][col] == ids.get(i)){
                                board[rows][col] = nextid;
                                Orphan_Block nextBlock = new Orphan_Block(rows,
                                            col,
                                            allBlocks.get(ids.get(i)).getColor());
                                currentBlocks.addFirst(nextBlock);
                                allBlocks.add(nextBlock);
                                nextid++;
                            }
                        }
                    }
                }

                //Now find the blocks that need to be shifted one space downward graphicail
                //This includes orhpaned blocks

                ids.clear();
                for(int rows = row - 1; rows > 0; rows--)
                {
                    for(int x = 0;x < board[rows].length; x++)
                    {
                        if(board[rows][x] != -1
                                && !ids.contains(board[rows][x]))
                        {
                            ids.add(board[rows][x]);
                        }
                    }
                }


                //Shift downward one row the vitural board
                for(int rows = row;rows > 0;rows--)
                    board[rows] = board[rows - 1];
                board[0] = new int[board[0].length];
                for(int i = 0; i< board[0].length;i++)
                    board[0][i] = -1;


                //Move all blocks down one row graphically
                Block moving;

                for(int i = 0;i<ids.size();i++)
                {
                    moving = allBlocks.get(ids.get(i));
                    moving.setY(moving.getY() + 10);
                }

              /*for(int t = 0;t<board.length;t++)
                {
                    for(int j = 0;j<board[t].length;j++)
                        System.out.print(", " + board[t][j]);
                    System.out.println();
                }
                System.out.println();
                System.out.println();*/


                //You have to check the same row to find if two lines were completed
                row++;
            }

            flag = true;
        }
        return numberFount;
    }
}
